﻿// Copyright (c) 2011 ECAlford
// This source is subject to the Microsoft Public License.
// See http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AceBots.ARPacket;
using AceBots.Xbee;

namespace XBeeARPacketController
{
    public partial class XBeeControllerForm : Form
    {
        public XbeeV1Module myXbeeModule;
        delegate void SetTextCallback(string text);
        public XbeeV1FrameMgr MyXbeeSndFrameMgr;
        public ARPacket myReceiveARPacket;
        public ARPacket mySendARPacket;
        public bool bReadTypeSet;
        public bool cblIncludeDate = false;
        public bool cblIncludeHeaderRow = false;
        public AceBots.FileMgr.ARPacketWriterFormats myFormat;
      
        public AceBots.Xbee.XBCallback myFrameCallback;
        
        
        public XBeeControllerForm()
        {
            InitializeComponent();
            myXbeeModule = new XbeeV1Module("COM15", 19200);
            myXbeeModule.FrameCallback = XbeeProcessReceivedPacket;
            

            AceBots.Windows.Utilities.ConfigData objConfig = new AceBots.Windows.Utilities.ConfigData
                ("c:\\users\\alfordec\\documents\\visual studio 2010\\Projects\\XBeeARPacketController\\XBeeARPacketController\\XbeeARPacketControllerConfig.xml");

            objConfig.AddItemListToComboBox(this.cmbPacketType,"ARPacketTypes");
            
            objConfig.AddItemListToComboBox(this.cmbARPacketCommands, "CommandList");
            objConfig.AddItemListToComboBox(this.cmbReadID, "ReadIDs");
            objConfig.AddItemListToComboBox(this.cmbXbeeAddr, "XbeeAddresses");
            objConfig.AddItemListToComboBox(this.cmbDataFormat, "FileDataFormat");
            


        }

   
        #region Added Functions Not Form Based



        private void CreatePacket()
        {
            MyXbeeSndFrameMgr = new XbeeV1FrameMgr(120);
           
            ARPacketTypes bPacketType;
            short iParam1=0;
            short iParam2=0;
            bool bHasParams = false;
           
            bPacketType = (ARPacketTypes)SelectedItemValue(this.cmbPacketType);
            if (txtParam1.Text.Length > 0)
            {
                iParam1 = Convert.ToInt16(txtParam1.Text);
                bHasParams = true;
            }
            if (txtParam1.Text.Length > 0) iParam2 = Convert.ToInt16(txtParam2.Text);
            switch (bPacketType)
            {
                case ARPacketTypes.ShortCommand:
                    if (bHasParams) mySendARPacket = new ARPacket(MyXbeeSndFrameMgr, myXbeeModule, ARPacketTypes.ShortCommand,
                         (ushort)SelectedItemValue(cmbARPacketCommands),iParam1,iParam2);
                    else
                        mySendARPacket = new ARPacket(MyXbeeSndFrameMgr, myXbeeModule, ARPacketTypes.ShortCommand,
                        (ushort)SelectedItemValue(cmbARPacketCommands));
                    break;
                case ARPacketTypes.VerboseCommand:
                       if (bHasParams) mySendARPacket = new ARPacket(MyXbeeSndFrameMgr, myXbeeModule,
                           ARPacketTypes.ShortCommand,
                         (ushort)SelectedItemValue(cmbARPacketCommands),
                         txtCommand.Text, iParam1, iParam2);
                    else
                        mySendARPacket = new ARPacket(MyXbeeSndFrameMgr, myXbeeModule, ARPacketTypes.ShortCommand,
                        (ushort)SelectedItemValue(cmbARPacketCommands),txtCommand.Text);
                    
                    break;
                case ARPacketTypes.BinaryReadings:
                    mySendARPacket = new ARPacket(MyXbeeSndFrameMgr, myXbeeModule, bPacketType);
                    break;
                case ARPacketTypes.BinaryShortReadings:
                    mySendARPacket = new ARPacket(MyXbeeSndFrameMgr, myXbeeModule, bPacketType);
                    break;
                case ARPacketTypes.BinarySingleReadType:
                    mySendARPacket = new ARPacket(MyXbeeSndFrameMgr, myXbeeModule, bPacketType);
                    break;
                default:
                    break;
            }
        }
        private short SelectedItemValue(ComboBox pcmbBox)
        {
            AceBots.Windows.Utilities.ShortValueItem objItem;
            objItem = (AceBots.Windows.Utilities.ShortValueItem)pcmbBox.SelectedItem;
            short ReturnVal = objItem.Value;
            return ReturnVal;
        }

        private void SetDataPacketVars()
        {
            if (mySendARPacket == null)
                CreatePacket();
            mySendARPacket.CurrentReadId = SelectedItemValue(cmbReadID);
        }

        public void XbeeProcessReceivedPacket(XbeeV1FrameMgr pFrameMgr)
        {
            // display packet on screen
            System.Text.StringBuilder sBuilder = new StringBuilder(500,2000);
            sBuilder.AppendLine("Packet Start");
           
            for (int i = 0; i < pFrameMgr.DataLength + 4; i++)
            {
                sBuilder.Append (i.ToString() + ", " + pFrameMgr.Buffer[i].ToString() + "\r\n");
               
            }
            SetText(sBuilder.ToString());
            // Log the data to a file
             ARPacket objPacket;
             if (cboxLogData.Checked)
             {
                 

                 objPacket = new ARPacket(pFrameMgr);
                 AceBots.FileMgr.ARPacketWriter objPacketWriter = new AceBots.FileMgr.ARPacketWriter(txtFileRoot.Text,
                     txtDir.Text, myFormat, cblIncludeDate,cblIncludeHeaderRow);
                 objPacketWriter.WriteData(objPacket);
             }
        }
        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.txtFrameData.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
                    
            }
            else
            {
                this.txtFrameData.AppendText( text);
            }
        }


 
       #endregion
        #region Form Event Handlers
        private void btnCreateNewPacket_Click(object sender, System.EventArgs e)
        {
           
            CreatePacket();
        }

        private void cmbPacketType_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            AceBots.Windows.Utilities.ShortValueItem objItem;
            ARPacketTypes bPacketType;
            objItem = (AceBots.Windows.Utilities.ShortValueItem)cmbPacketType.SelectedItem;
            bPacketType = (ARPacketTypes)objItem.Value;
            btnCreateNewPacket.Visible = true;
            switch (bPacketType)
            {
                case ARPacketTypes.ShortCommand:
                    cmbARPacketCommands.Enabled = true;
                    groupBox1.Visible = true;
                    groupBox2.Visible = false;
                    groupBox4.Visible = false;
                    btnCreateNewPacket.Visible = false;
                    break;
                case ARPacketTypes.VerboseCommand:
                    cmbARPacketCommands.Enabled = true;
                    groupBox1.Visible = true;
                    groupBox2.Visible = false;
                    groupBox4.Visible = true;
                    btnCreateNewPacket.Visible = false;
                    break;
                case ARPacketTypes.BinaryReadings:
                    cmbARPacketCommands.Enabled = false;
                    groupBox1.Visible = false;
                    groupBox2.Visible = true;
                    groupBox3.Visible = true;
                    CreatePacket();
                    break;
                case ARPacketTypes.BinaryShortReadings:
                    cmbARPacketCommands.Enabled = false;
                    groupBox1.Visible = false;
                    groupBox2.Visible = true;
                    groupBox3.Visible = false;
                    CreatePacket();
                    break;
                case ARPacketTypes.BinarySingleReadType:
                    cmbARPacketCommands.Enabled = false;
                    groupBox1.Visible = false;
                    groupBox2.Visible = true;
                    groupBox3.Visible = true;
                    CreatePacket();
                    break;
                default:
                    break;
            }

        }


        private void btnAddShort_Click(object sender, EventArgs e)
        {
            if (mySendARPacket == null)
                CreatePacket();
            if (txtShortVal.Text.Length > 0)
                mySendARPacket.AppendData((ushort)SelectedItemValue(cmbReadID), (short)Convert.ToInt16(txtShortVal.Text));

        }

        private void btnSendPacket_Click(object sender, EventArgs e)
        {
            if (mySendARPacket == null)
                CreatePacket();
            else if (mySendARPacket.PacketType == ARPacketTypes.ShortCommand ||
                mySendARPacket.PacketType == ARPacketTypes.VerboseCommand)
                CreatePacket();
            MyXbeeSndFrameMgr.SendAddress = SelectedItemValue(cmbXbeeAddr);
            mySendARPacket.CalcCheckSumAndLength();
            myXbeeModule.SendAPIPacket(MyXbeeSndFrameMgr);
        }

        private void btnParseSendPacket_Click(object sender, EventArgs e)
        {
            string sVal;
            txtFrameData.Text = " ";
            for (int i = 0; i < MyXbeeSndFrameMgr.DataLength+4; i++)
            {
                sVal = (i.ToString() + ", " + MyXbeeSndFrameMgr.Buffer[i].ToString() + "\r\n");
                txtFrameData.AppendText(sVal);
            }
        }
#endregion

        private void btnAppendString_Click(object sender, EventArgs e)
        {
             if (mySendARPacket == null)
                CreatePacket();
             if (txtStringVal.Text.Length > 0)
                 mySendARPacket.AppendData((ushort)SelectedItemValue(cmbReadID), txtStringVal.Text);

        }

        private void btnAppendInt_Click(object sender, EventArgs e)
        {
             if (mySendARPacket == null)
                CreatePacket();
             if (txtInt.Text.Length > 0)
                 mySendARPacket.AppendData((ushort)SelectedItemValue(cmbReadID), (int)Convert.ToInt32(this.txtInt.Text));
        }

        private void btnAppendByte_Click(object sender, EventArgs e)
        {
            if (mySendARPacket == null)
                CreatePacket();
            if (txtByte.Text.Length > 0)
                mySendARPacket.AppendData((ushort)SelectedItemValue(cmbReadID),(byte) Convert.ToSByte(this.txtByte.Text));

        }

        private void cmbDataFormat_SelectedIndexChanged(object sender, EventArgs e)
        {
            myFormat = (AceBots.FileMgr.ARPacketWriterFormats)
                     SelectedItemValue(this.cmbDataFormat);
        }

        private void cbxIncludeTimestamp_CheckedChanged(object sender, EventArgs e)
        {
            cblIncludeDate= cbxIncludeTimestamp.Checked;
        }

        private void cbxIncludeHeader_CheckedChanged(object sender, EventArgs e)
        {
            cblIncludeHeaderRow = cbxIncludeHeader.Checked;
        }
       




      
    }
}
